//
//  AppDelegate+Eques.m
//  AlinkDemo
//
//  Created by Vincent on 2017/10/29.
//  Copyright © 2017年 aliyun. All rights reserved.
//

#import "AppDelegate+Eques.h"
#import "CallViewController.h"
#import <Reachability.h>
#import "Constant.h"
#import <EquesBusiness/YKBusinessFramework.h>
#import <AlinkSDK/AlinkAccount.h>
@implementation AppDelegate (Eques)

- (void)initEques{

    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeGradient];
    [SVProgressHUD setMinimumDismissTimeInterval:2];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMessageResult:) name:equesOnMessageResultNotification object:nil];

    [self reachability];
}

- (void)reachability
{
    // 1.获得网络监控的管理者
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        
        if (![YKBusinessFramework equesIsLogin]) {
            
            NSDictionary *session =  [[AlinkAccount sharedInstance] currentSession];
            NSString *userName = session[@"ak_account"];
            if (userName) {
                [YKBusinessFramework equesSdkLoginWithUrl:SERVERADDRESS username:userName appKey:APPKEY];
            }
            
            
        }
       

        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                NSLog(@"未知网络");
                break;
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                NSLog(@"没有网络(断网)");
                [SVProgressHUD showInfoWithStatus:@"当前没有网络"];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                NSLog(@"手机自带网络");
//                [SVProgressHUD showInfoWithStatus:@"正在使用数据网络"];

                break;
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                NSLog(@"WIFI");
//                [SVProgressHUD showInfoWithStatus:@"正在使用Wifi"];

                break;
        }
    }];
    // 3.开始监控
    [mgr startMonitoring];
}

#pragma mark 收到的消息
- (void)onMessageResult:(NSNotification *)notification{
    //为登录SDS直接return
    if(![[AlinkAccount sharedInstance] isLogin])return;

    NSDictionary *messageResult = [notification object];

    NSString *method = messageResult[@"method"];

    if ([method isEqualToString:@"call"]) {

        if (!self.isPlaying) {
            NSString *from = messageResult[@"from"];
            
//            if (self.isEyesOpen) {
//                [[NSNotificationCenter defaultCenter] postNotificationName:@"ShowCallViewNotificaion" object:from];
//
//            }else{
//                UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//                CallViewController *vc = [sb instantiateViewControllerWithIdentifier:@"Call"];
//                vc.bid = from;
//                vc.isDoolCall = YES;
//                [self.window.rootViewController presentViewController:vc animated:NO completion:nil];
//            }

            [[self.window.rootViewController presentedViewController] dismissViewControllerAnimated:NO completion:nil];

            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            CallViewController *vc = [sb instantiateViewControllerWithIdentifier:@"Call"];
            vc.bid = from;
            vc.isDoolCall = YES;
            [self.window.rootViewController presentViewController:vc animated:NO completion:nil];

        }

    }
    
}

-(void)YK_applicationWillEnterForeground:(UIApplication *)application{

    
    if (![YKBusinessFramework equesIsLogin]) {
        NSDictionary *session =  [[AlinkAccount sharedInstance] currentSession];
        NSString *userName = session[@"ak_account"];
        if (userName) {
            [YKBusinessFramework equesSdkLoginWithUrl:SERVERADDRESS username:userName appKey:APPKEY];
        }
    }
}

@end
